package org.jboss.seam.example.divo;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jboss.seam.Component;
import org.jboss.seam.example.divo.domain.Booking;
import org.jboss.seam.example.divo.domain.Hotel;
import org.jboss.seam.example.divo.service.BookingService;
import org.jboss.seam.example.divo.service.HotelService;

import com.divo.core.json.JSONDataReader;
import com.divo.core.json.JSONUtil;
import com.divo.core.misc.BaseServlet;
import com.divo.core.misc.GridParam;
import com.divo.core.misc.ListRange;
import com.divo.core.util.StringUtil;

public class SampleServlet extends BaseServlet {

    private HotelService hotelService;

    private void createHotelService() throws ServletException {
	hotelService = (HotelService) Component.getInstance(HotelService.class);
    }

    private BookingService bookingService;

    private void createBookingService() throws ServletException {
	bookingService = (BookingService) Component.getInstance(BookingService.class);
    }
    
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        createHotelService();
        createBookingService();

        String action = request.getParameter("action");
        
        if (action.equals("hotelList")) 
            runHotelList(request, response);
        
        if (action.equals("bookingList")) 
            runBookingList(request, response);

    }

    /**
     * Booking List
     * @param request
     * @param response
     * @throws IOException 
     */        
    private void runBookingList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        GridParam param = new GridParam(request);

        ListRange<Booking> lr = bookingService.findAll(param);

        JSONDataReader jsonReader = new JSONDataReader();
        jsonReader.setRecordCount(lr.getTotalSize());

        if (lr.getData().size() > 0) {
            for (Booking obj : lr.getData()) { 
                jsonReader.put(obj.toJSONObject());
            }
        }
    
        JSONUtil.buildJSON(request, response, jsonReader);
    }

    /**
     * Hotel List
     * 
     * @param request
     * @param response
     * @throws IOException 
     */
    private void runHotelList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        GridParam param = new GridParam(request);
        String filter = StringUtil.notNull(request.getParameter("filter"));

        ListRange<Hotel> lr = hotelService.findAll(param, filter);

        JSONDataReader jsonReader = new JSONDataReader();
        jsonReader.setRecordCount(lr.getTotalSize());

        if (lr.getData().size() > 0) {
            for (Hotel obj : lr.getData()) { 
                jsonReader.put(obj.toJSONObject());
            }
        }
    
        JSONUtil.buildJSON(request, response, jsonReader);
    }

}
